package StudyCode.summary.mycollection;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class CustomIterable implements Iterable<Stock> {

	Map<String, Stock> portfolio = new HashMap<>();

	public void add(Stock stock) {
		portfolio.put(stock.getSymbol(), stock);
	}

	public void add(List<Stock> stocks) {
		for (Stock s : stocks) {
			portfolio.put(s.getSymbol(), s);
		}
	}

	@Override
	public Iterator<Stock> iterator() {
		return portfolio.values().iterator();
	}

	public static void main(String[] args) {
		CustomIterable myPortfolio = new CustomIterable();
		myPortfolio.add(new Stock("ORCL", "Oracle", 5010.0));
		myPortfolio.add(new Stock("AAPL", "Apple", 2010.0));
		myPortfolio.add(new Stock("GOOG", "Google", 1100.0));
		myPortfolio.add(new Stock("IBM", "IBM", 510.0));
		myPortfolio.add(new Stock("MCD", "McDonalds", 3010.0));

		for (Stock stock : myPortfolio) {
			System.out.println(stock);
		}

		myPortfolio.forEach(s -> System.out.println(s));
	}
}

class UnmodifiableIterator {
	public static <E> Iterator<E> wrap(final Iterator<E> iter) {
		return new Iterator<E>() {

			@Override
			public boolean hasNext() {
				return iter.hasNext();
			}

			@Override
			public E next() {
				return iter.next();
			}

			@Override
			public void remove() {
				throw new UnsupportedOperationException("Remove not supported.");
			}
		};
	}
}

class Stock {
	private String symbol;
	private String name;
	private double shares;

	public Stock(String symbol, String name, double shares) {
		this.symbol = symbol;
		this.name = name;
		this.shares = shares;
	}

	public String getSymbol() {
		return symbol;
	}

	public String getName() {
		return name;
	}

	public double getShares() {
		return shares;
	}

	public String toString() {
		return shares + " shares of " + symbol + " (" + name + ")";
	}
}
